package com.chatdating.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chatdating.entity.Post;
import com.chatdating.entity.dto.AdminPostCommentDto;
import com.chatdating.entity.dto.AdminPostDto;
import com.chatdating.entity.pojo.Result;
import com.chatdating.service.CommentService;
import com.chatdating.service.PostService;
import com.chatdating.service.UserService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminCommunityController {

    private static final Logger logger = LoggerFactory.getLogger(AdministratorController.class);

    @Autowired
    private PostService postService;

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    /*
    社区帖子回显
     */
    @GetMapping("/community")
    public ResponseEntity<Result> community(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "3") Integer pageSize,
            @RequestParam(required = false) String uname,
            @RequestParam(required = false) Integer id
            ){
        try {
            PageInfo<AdminPostDto> pageInfo = postService.AdminPostList(pageNum, pageSize, uname, id);
            return ResponseEntity.ok(Result.success(pageInfo));
        } catch (Exception e) {
            logger.error("获取帖子信息失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error(500,"系统错误，请稍后再试"));
        }
    }
    @GetMapping("/delPostList")
    public ResponseEntity<Result> delPostList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "3") Integer pageSize,
            @RequestParam(required = false) String uname,
            @RequestParam(required = false) Integer id
    ){
        try {
            PageInfo<AdminPostDto> pageInfo = postService.delPostList(pageNum, pageSize, uname, id);
            return ResponseEntity.ok(Result.success(pageInfo));
        } catch (Exception e) {
            logger.error("获取帖子信息失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error(500,"系统错误，请稍后再试"));
        }
    }
    /*
    查看帖子（评论）
     */
    @GetMapping("/getPost")
    public ResponseEntity<Result<List<Post>>> getPost(@RequestParam("id") Long id) {
        try {
            List<Post> post = postService.findById(id);
            if (post == null || post.isEmpty()) {
                return ResponseEntity.ok(Result.success(Collections.emptyList()));
            }
            return ResponseEntity.ok(Result.success(post));
        } catch (Exception e) {
            logger.error("获取帖子信息失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error(500,"系统错误，请稍后再试"));
        }
    }
    /*
    删除贴子
     */
    @GetMapping("/deletePost")
    public void deletePost(@RequestParam("id") int id) {
        postService.adminRemoveById(id);
    }
    /*
    恢复贴子
     */
    @GetMapping("/recoverPost")
    public void recoverPost(@RequestParam("id") int id) {
        postService.adminRecoverPostById(id);
    }

    /*
    回显帖子详情(包括父子评论)点赞
     */
    @GetMapping("/postDetails")
    public ResponseEntity<Result<AdminPostCommentDto>> getPostDetails(@RequestParam("id") Long id) {
        try {
            Post post = postService.getPostWithByid(id);
            if (post == null) {
                return ResponseEntity.ok(Result.success(null));
            }
            String uname = userService.getNameById(post.getAuthorId());
            AdminPostCommentDto adminPostCommentDto = new AdminPostCommentDto(
                    post.getLikeCount(),
                    uname,
                    post.getContent(),
                    post.getCreateTime().toString(),
                    post.getUpdateTime().toString(),
                    post.getImageUrls(),
                    post.getComments(),
                    post.getCommentCount()
            );
            return ResponseEntity.ok(Result.success(adminPostCommentDto));
        } catch (Exception e) {
            logger.error("获取帖子信息失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error(500,"系统错误，请稍后再试"));
        }
    }
    @GetMapping("/delComment")
    public ResponseEntity<Result> delComment(
            @RequestParam("commentId") Integer id
    ){
        try {
            commentService.delComment(id);
            return ResponseEntity.ok(Result.success());
        } catch (Exception e) {
            logger.error("删除评论失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error(500,"系统错误，请稍后再试"));
        }
    }


}
